package com.tourism.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tourism.dao.LineDao;
import com.tourism.dao.UserDao;
import com.tourism.entity.Line;
import com.tourism.entity.LineQuery;
import com.tourism.exception.ExceptionEnum;
import com.tourism.exception.GlobalException;
import com.tourism.service.LineService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Description
 * @Author z
 * @Date 2021-03-01 21:05
 */
@Service
public class LineServiceImpl implements LineService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private LineDao lineDao;

//根据ID查询线路
    @Override
    public Line getLineById(Long id) {
        Line line = lineDao.getLineById(id);
        return line;
    }

//增加线路
    @Override
    public boolean addLine(Line line) throws GlobalException {
        if (existLine(line.getName(), null)) {
            throw new GlobalException(ExceptionEnum.LineExist);
        }
//        获取创建时间和ID
        line.init();
        return lineDao.addLine(line);
    }

    @Override
    public boolean existLine(String name, String id) {
        boolean exist;
        Line line = lineDao.getLineByName(name);
        if (StringUtils.isBlank(id)) {
            exist = line != null;
        } else {
            exist = !id.equals(line.getId());
        }
        return exist;
    }
//分页查询线路
    @Override
    public PageInfo<Line> getLineList(LineQuery lineQuery) {
        PageHelper.startPage(lineQuery.getPageNum(), lineQuery.getPageSize());
        List<Line> lineList = lineDao.getLineList(lineQuery);
        PageInfo<Line> result = new PageInfo<>(lineList);
        return result;
    }

    @Override
    public Boolean deleteLineById(Long id) {
        return lineDao.deleteLineById(id);
    }

    @Override
    public boolean updateLine(Line line) {
        return lineDao.updateLine(line);
    }


}
